// cloudfunctions/getSellerInfo/index.js
const cloud = require('wx-server-sdk');
cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
  })
const db = cloud.database();

exports.main = async (event, context) => {
  try {
    const { sellerId } = event;
    if (!sellerId) {
      return { success: false, message: '缺少卖家ID' };
    }
    
    // 从users集合获取卖家信息
    const userResult = await db.collection('users').doc(sellerId).get();
    if (!userResult.data) {
      return { success: false, message: '卖家信息不存在' };
    }
    
    const sellerInfo = userResult.data;
    return {
      success: true,
      data: {
        _id: sellerInfo._id,
        name: sellerInfo._nickName  || '未设置用户名',
        avatar: sellerInfo._avatarUrl || '',
        goodsCount: sellerInfo.goodsCount || 0
      }
    };
  } catch (error) {
    console.error('获取卖家信息失败', error);
    return { success: false, message: '服务器错误', error: error.message };
  }
};